package Ali;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 给定一个 没有重复 数字的序列，返回其所有可能的全排列。
 * @author le
 */
public class Test08
{
    public static void main(String[] args)
    {
        int[] num = {1, 2, 3};
        int len = num.length;
        List<List<Integer>> res = new ArrayList<>();

        Stack<Integer> path = new Stack<>();
        boolean[] used = new boolean[len];
        int dept = 0;

        dfs(num, len, dept, path,used,res);
        for (int i = 0;i < res.size();i++){
            System.out.println(res.get(i));
        }
    }

    private static void dfs(int[] num, int len, int dept, Stack<Integer> path,boolean[] used,List<List<Integer>> res)
    {
        if (dept == len){
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = 0;i < num.length;i++){
            if (used[i]){
                continue;
            }
            used[i] = true;
            path.add(num[i]);
            dfs(num,len,dept+1,path,used,res);
            used[i] = false;
            path.pop();
        }

    }

}
